// pages/unlock/unlock.js
// pages/meeting/apply/apply.js
var api = require('../../utils/httpUtil.js');
var dateUtils = require('../../utils/dateUtils.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: -1,
    doors: [],
    openId: app.globalData.openId
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.loadDoorData();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  //加载会议室信息
  loadDoorData() {
    api.httpRequest(api.doorListAPI, {
        openId: this.data.openId
      })
      .then(res => {
        if (res.code == 200) {
          this.setData({
            doors: res.data
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 2000,
            mask: true,
            icon: 'none'
          })
        }
      })
      .catch(error => {
        wx.showToast({
          title: error.message,
          duration: 1000,
          mask: true,
          icon: 'none'
        })
      });
  },

  searchInput: function (e) {
    this.setData({
      searchText: e.detail.value
    })
  },

  searchClick: function (e) {
    var query = this.data.searchText;
    var doorList = this.data.doors.filter(item => item.name.includes(query));
    if (doorList.length > 0) {
      var dName = doorList[0].name;
      var dId = doorList[0].liId;
      var selIndex = this.data.doors.findIndex(item => item.name === dName);
      this.setData({
        index: selIndex,
        doorName: dName,
        liId: dId
      })
    } else {
      this.setData({
        index: -1,
        doorName: '',
        liId: ''
      })
    }
  },

  doorChange: function (e) {
    this.setData({
      index: e.detail.value,
      liId: this.data.doors[e.detail.value].liId,
      doorName: this.data.doors[e.detail.value].name
    });
  },

  submit() {
    if (this.data.index == -1) {
      wx.showToast({
        title: '请选择门禁',
        duration: 1000,
        mask: true,
        icon: 'none'
      })
      return;
    }
    api.httpRequest(api.doorOpenAPI, {
        liId: this.data.liId,
        openId: this.data.openId,
      })
      .then(res => {
        wx.showToast({
          title: res.msg,
          duration: 1000,
          mask: true,
          icon: 'none',
          success: function () {
            if (res.code == 200) {
              setTimeout(function () {
                wx.navigateBack();
              }, 1000);
            }
          }
        })
      })
      .catch(error => {
        wx.showToast({
          title: error.message,
          duration: 1000,
          mask: true,
          icon: 'none'
        })
      });
  }
})